<html>
<head><meta charset="utf-8"><title>HashMap::with_capacity · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html">HashMap::with_capacity</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223059446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059446">(Jan 17 2021 at 19:43)</a>:</h4>
<p>HashMap::with_capacity is implemented for RandomState only, but not for <code>BuildHasher + Default</code>. This makes using FxHashMap, and any other case with a custom hasher impl more cumbersome. Well I'm sure there is a reason behind this, but does anyone know what that reason is?</p>



<a name="223059808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059808">(Jan 17 2021 at 19:51)</a>:</h4>
<p>I think it's something to do with default type parameters not being used in inference</p>



<a name="223059815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059815">(Jan 17 2021 at 19:51)</a>:</h4>
<p>So if it was generic, then <code>HashMap::with_capacity(4)</code> would always complain that you need to specify the hasher.</p>



<a name="223059868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059868">(Jan 17 2021 at 19:52)</a>:</h4>
<p>That's interesting. Is that by design, or inference being not sophisticated enough?</p>



<a name="223059947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059947">(Jan 17 2021 at 19:54)</a>:</h4>
<p>Not sure <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>
<p>I think it gets into the territory of fallbacks, which is hard to have with inference.  Because if you do <code>let FxHashMap&lt;_, _&gt; = HashMap::default()</code>, then you want the Hasher to be inferred, not defaulted.</p>



<a name="223059995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/HashMap%3A%3Awith_capacity/near/223059995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dániel Buga <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/HashMap.3A.3Awith_capacity.html#223059995">(Jan 17 2021 at 19:56)</a>:</h4>
<p>Uh, yeah, you're right. I guess it's a problem that seems easier than it actually is.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>